<?php
class Zendvn_Plugin_Permission extends Zend_Controller_Plugin_Abstract
{ 
	public function preDispatch(Zend_Controller_Request_Abstract $request)
	{
		$auth = Zend_Auth::getInstance();
		$ns = new Zend_Session_Namespace('info');
		
		$moduleName = strtolower($this->_request->getModuleName());
		$action  = strtolower($this->_request->getActionName());
		$flagAdmin = false;
		
		if ($moduleName == 'admincpanel')
		{
			$flagAdmin = true;
		}
			
		$flagPage = 'none';
		if ($flagAdmin == true)
		{
			if ( ! in_array($action, array('pass-recover', 'auth')))
			{
				if ($auth->hasIdentity() == false)
				{
					$flagPage = 'login';
				}
				else
				{
					$info = new Zendvn_System_Info();
					$permission  = $info->getGroupInfo($auth->getIdentity());
					if ($permission['permission'] != 'Full Access' AND ! in_array($action, array('action', 'auth', 'scripts', 'logout', 'change-pass')))
					{
						$role = $ns->acl['privileges'];
						if ( ! $role OR $role == '')
						{
							$flagPage = 'no-access';
						}
						else
						{
							$acl = new Zendvn_System_Acl();
							$arrParam = $this->_request->getParams();
							if ($acl->isAllowed($arrParam) == false)
							{
								$flagPage = 'no-access';
							}
						}
					}
				}
			}
		}
		
		//print_r($ns->acl);
		//----------END-KIEM TRA QUYEN TRUY CAP VAO ADMIN -------------
		if ($flagPage != 'none')
		{
			if ($flagPage == 'login')
			{
				$this->_request->setModuleName('admincpanel');
				$this->_request->setControllerName('public');
				$this->_request->setActionName('login');
			}
			
			if ($flagPage == 'no-access')
			{
				$this->_request->setModuleName('admincpanel');
				$this->_request->setControllerName('public');
				$this->_request->setActionName('no-access');
			}
		}
	}
}